package me.whf.serivce;

import java.util.Collections;
import java.util.List;

import me.whf.dao.EmployeeDAO;
import me.whf.dao.LoggerDAO;
import me.whf.dao.factory.DAOFactory;
import me.whf.model.Employee;
import me.whf.model.Logger;
import me.whf.util.Utils.EmployeeType;

public class EmployeeService {

	public Employee login(String employeeID, String password) {
		EmployeeDAO employeeDAO = DAOFactory.getDAOFactory().getEmployeeDAO();
		return employeeDAO.login(employeeID, password);
	}
	
	public boolean register(String employeeID, String password, String username, String apartmentID, EmployeeType employeeType) {
		EmployeeDAO employeeDAO = DAOFactory.getDAOFactory().getEmployeeDAO();
		
		if (employeeDAO.isExisted(employeeID))
			return false;
		else
			return employeeDAO.register(employeeID, password, username, apartmentID, employeeType);
	}
	
	public String reportGenerate(EmployeeType employeeType, String employeeID, String apartmentID, String startDate, String endDate) {
		LoggerDAO loggerDAO = DAOFactory.getDAOFactory().getLoggerDAO();
		List<Logger> reports = loggerDAO.queryByEmployee(employeeType, employeeID, apartmentID, startDate, endDate);
		
		Collections.sort(reports);
		StringBuilder sb = new StringBuilder();
		for (Logger logger: reports) {
			sb.append(logger.getOperatorID() + " " + logger.getLogDate() + " " + logger.getContent() + '\n');
		}
		return sb.toString();
	}
}
